package util;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.swing.JOptionPane;

/**
 * The exception handler for the Swing thread.
 * @author TKington
 *
 */
public class ExceptionHandler {
	/**
	 * Displays a message box, and logs the Throwable.
	 * @param t the Throwable
	 */
    public void handle(Throwable t) {
        JOptionPane.showMessageDialog(null, "An exception occurred.  See viewer.log for details");
        logSilent(t);
    }
    
    /**
     * Silently logs the Throwable to viewer.log.
     * @param t the Throwable
     */
    public void logSilent(Throwable t) {
        try {
            PrintWriter out = new PrintWriter(new FileWriter("viewer.log", true)); //$NON-NLS-1$
            out.println(new Date() + ":"); //$NON-NLS-1$
            
            t.printStackTrace(out);
            out.close();
        }
        catch(IOException e) {
            System.out.println("Error logging exception to viewer.log");
            e.printStackTrace();
            t.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error logging exception to viewer.log");
        }
    }
}